﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0712502_0712504_DesignPatternGame
{
    public abstract class UserControlledTank : TankPrototype
    {
        #region Attribute
        ////State////
        protected UserControlledTankManager _tankManager;

        public UserControlledTankManager TankManager
        {
            get { return _tankManager; }
            //set { _tankManager = value; }
        }

        protected int _exp;//diem kinh nghiem        
        public int Exp
        {
            get { return _exp; }
            set { _exp = value; }
        }

        protected float distance = 0;
        protected int _lowerExp;
        protected int _upperExp;
        public int ReloadTime;
        public int TimeDelayToReload = 0;

        /////end State///////

        protected TankBodyFactory tankBodyFactory;
        protected TankTurretFactory tankTurretFactory;

        #endregion

        #region Operation
        /////State/////
        public abstract void AddExperiencePoint(int exp);
        //////////////

        public override void DrawToScreen(GameTime gameTime, SpriteBatch spriteBatch)
        {            
            spriteBatch.Draw(GameResourceContainer.ImageManager.rect54, this.RectCollisionOutside, Color.White);
            this._bodyTank.DrawToScreen(gameTime, spriteBatch);
            this._turretTank.DrawToScreen(gameTime, spriteBatch);
            this._bulletManager.DrawToScreen(gameTime, spriteBatch);           
        }
        #endregion

               
        public override void TankUpdate(GameTime gameTime)
        {
            MouseState aCurrentMouseState = Mouse.GetState();
                             
            this.UpdateMouseMove(aCurrentMouseState);
            this._bulletManager.Update(gameTime);   // Cập nhật lại đạn
            
            this.ProcessKeyBoard();
            this.UpdateTankMoving();
            
            this.BodyTank.RotationAngle = this.RotationAngle;            // Góc vẽ của Body      
            this._turretTank.RotationAngle = this.TurretRotationAngle;   // Góc vẽ của Turret
            
            GlobalVariables.playerEXP = this.Exp;   // Lay diem kinh nghiem cua player tank
            GlobalVariables.playerCurHP = this.CurrentHP;
            GlobalVariables.playerMaxHP = this.MaxHP;
            GlobalVariables.playerLife = this.Life;
            base.TankUpdate(gameTime);  // Cập nhật vị trí của Body và Turret
        }

        public void UpdateTankMoving()
        {
            if (CheckCollision() == true)
                this.MoveState = MovingState.Stop;

            if (this.MoveState == MovingState.Run)
            {
                if (distance > 54 + ISpeed)
                {
                    this.MoveState = MovingState.Stop;
                    distance = 0;
                }
                this.Move();
                distance += ISpeed;
            }
        }

        public void UpdateMouseMove(MouseState aCurrentMouseState)
        {
            Vector2 mousePosition = new Vector2(aCurrentMouseState.X, aCurrentMouseState.Y);
            TurretRotationAngle = -(float)Math.Atan2(this._position.X - mousePosition.X, _position.Y - mousePosition.Y);
                     
            if (ReloadTime > TimeDelayToReload)
            {
                if (aCurrentMouseState.LeftButton == ButtonState.Pressed)
                {
                    TankShot();
                }
                ReloadTime = 0;
            }
            ReloadTime++;
           
        }

        public virtual void Initialize() { }        // Sẽ được override trong các lớp UCTLeve11, 2, 3
        
        public void ProcessKeyBoard()
        {
            KeyboardState keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.A))  // move right
            {
                if (_movingDirection != MoveDirectionState.left)
                {
                    _movingDirection = MoveDirectionState.left;
                  
                }

                else
                {
                    this.MoveState = MovingState.Run;
                }
            }

            if (keyboardState.IsKeyDown(Keys.D))  // move right
            {
                if (_movingDirection != MoveDirectionState.right)
                {
                    _movingDirection = MoveDirectionState.right;                    
                  
                }
                    
                else
                {
                    this.MoveState = MovingState.Run;
                }
            }

            if (keyboardState.IsKeyDown(Keys.W)) // move up
            {
                if (_movingDirection != MoveDirectionState.up)
                {
                    _movingDirection = MoveDirectionState.up;                    
                  
                }
                    
                else
                {             
                    this.MoveState = MovingState.Run;                                  
                }
            }

            if (keyboardState.IsKeyDown(Keys.S))     // move down
            {
                if (_movingDirection != MoveDirectionState.down)
                {
                    _movingDirection = MoveDirectionState.down;                    
                  
                }
                    
                else
                {                  
                    this.MoveState = MovingState.Run;                                 
                }
            }

            if (keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.D)
                && keyboardState.IsKeyUp(Keys.W) && keyboardState.IsKeyUp(Keys.S))
            {
               
            }          
        }

       
        public void AdjustTankPosition()
        {
            int deltaX =(int) this.Position.X % 25;
            int deltaY =(int) this.Position.Y % 25;
            Vector2 tempPos = this._position;
            if (deltaX <= 8)
            {
                tempPos.X = tempPos.X - deltaX;
                this.Position = tempPos;
                
                if (CheckCollisionTank_Tank() == true)
                {
                    tempPos.X = tempPos.X + deltaX;
                    this.Position = tempPos;
                }
            }

            else
            {
                tempPos.X += (25 - deltaX);
                this.Position = tempPos;
                
                if (CheckCollisionTank_Tank() == true)
                {
                    tempPos.X -= (25 - deltaX);
                    this.Position = tempPos;
                }
            }

            if (deltaY <= 8)
            {
                tempPos.Y = tempPos.Y - deltaY;
                this.Position = tempPos;
                
                if (CheckCollisionTank_Tank() == true)
                {
                    tempPos.Y = tempPos.Y + deltaY;
                    this.Position = tempPos;
                }
            }

            else
            {
                tempPos.Y += (25 - deltaY);
                this.Position = tempPos;
                
                if (CheckCollisionTank_Tank())
                {
                    tempPos.Y -= (25 - deltaY);
                    this.Position = tempPos;
                }
            }
        }
    }
}
